package cn.b.sky.mes.andon.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;

import cn.b.sky.mes.andon.dao.AndonAttributeDao;
import cn.b.sky.mes.andon.model.AndonAttribute;
import cn.b.sky.mes.andon.model.AndonAttributeEnum;
import cn.b.sky.mes.andon.model.condition.AndonAttributeCondition;
import cn.b.sky.mes.andon.service.AndonAttributeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("andonAttributeService")
public class AndonAttributeServiceImpl implements AndonAttributeService {
  
  private static final Logger LOG = LoggerFactory.getLogger(AndonAttributeServiceImpl.class);

  @Resource
  AndonAttributeDao andonAttributeDao;

  @Override
  public List<AndonAttribute> selectAndonAttributeByCondition(AndonAttributeCondition andonAttributeCondition) {
    if (null == andonAttributeCondition) {
      return null;
    }
    return andonAttributeDao.selectAndonAttributeByCondition(andonAttributeCondition);
  }

  @Override
  public int selectAndonAttributeCount(AndonAttributeCondition andonAttributeCondition) {
    if (null == andonAttributeCondition) {
      return 0;
    }
    return andonAttributeDao.selectAndonAttributeCount(andonAttributeCondition);
  }

  @Override
  public AndonAttribute selectAndonAttribute(String siteCode, String attributeCode) {
    if (StringUtils.isBlank(attributeCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return andonAttributeDao.selectAndonAttribute(siteCode, attributeCode);
  }

  @Override
  public void insertAndonAttribute(AndonAttribute andonAttribute) {
    if (andonAttribute != null) {
      andonAttributeDao.insertAndonAttribute(andonAttribute);
      if (andonAttribute.getAttributeEnumList() != null) {
        for (AndonAttributeEnum attributeEnum : andonAttribute.getAttributeEnumList()) {
          andonAttributeDao.insertAndonAttributeEnum(attributeEnum);
        }
      }
    }

  }

  @Override
  public void updateAndonAttribute(AndonAttribute andonAttribute) {
    if (andonAttribute != null) {
      andonAttributeDao.updateAndonAttribute(andonAttribute);
      andonAttributeDao.deleteAndonAttributeEnum(andonAttribute.getSiteCode(), andonAttribute.getAttributeCode());
      if (andonAttribute.getAttributeEnumList() != null) {
        for (AndonAttributeEnum attributeEnum : andonAttribute.getAttributeEnumList()) {
          andonAttributeDao.insertAndonAttributeEnum(attributeEnum);
        }
      }
    }

  }

  @Override
  public BatchJobReport deleteAndonAttribute(String siteCode, String attributeCode) {
    if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(attributeCode)) {
      return new BatchJobReport("", "siteCode or attributeCode is null", false);
    }
    String message = attributeCode;
    boolean success = true;
    String cause = "";
    try {
      andonAttributeDao.deleteAndonAttributeEnum(siteCode, attributeCode);
      andonAttributeDao.deleteAndonAttribute(siteCode, attributeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success);
  }

  @Override
  public List<AndonAttributeEnum> selectAndonAttributeEnumByAttribute(String siteCode, String attributeCode) {
    if (StringUtils.isBlank(attributeCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return andonAttributeDao.selectAndonAttributeEnumByAttribute(siteCode, attributeCode);
  }

  @Override
  public List<AndonAttribute> selectAndonAttributeAndEnumsListByAndonType(String siteCode, String andonType) {
    List<AndonAttribute> andonAttributeList= this.andonAttributeDao.selectAndonAttributeListByAndonType(siteCode,andonType);
    if(!CollectionUtils.isEmpty(andonAttributeList)){
      Map<String,AndonAttribute> andonAttributeMap = new HashMap<String,AndonAttribute>();
      for(AndonAttribute andonAttribute : andonAttributeList){
        andonAttributeMap.put(andonAttribute.getAttributeCode(), andonAttribute);
      }
      
      List<AndonAttributeEnum> andonAttributeEnumList = this.andonAttributeDao.selectAndonAttributeEnumListByAndonType(siteCode, andonType);
      AndonAttribute andonAttribute = null;
      if(!CollectionUtils.isEmpty(andonAttributeEnumList)){
        for(AndonAttributeEnum andonAttributeEnum : andonAttributeEnumList){
          andonAttribute = andonAttributeMap.get(andonAttributeEnum.getAttributeCode());
          if(null != andonAttribute){
            andonAttribute.addAttributeEnum(andonAttributeEnum);
          }
        }
      }
      
    }
    return andonAttributeList;
  }

}
